package cn.edu.jxau.test;

import cn.edu.jxau.util.LinkedStack;
import cn.edu.jxau.util.Stack;

/**
 * 补全表达式中缺失的括号
 * @author 付大石
 */
public class Main {

    public static void main(String[] args) {
        
        String[] exp = {"1","+","2",")","*","3","-","4",")","*","5","-","6",")",")",")"};
        Stack<String> valueStack = new LinkedStack<>();
        Stack<String> operationStack = new LinkedStack<>();
        for(String item : exp) {
            if(item.equals("+")) {
                operationStack.push(item);
            } else if(item.equals("-")) {
                operationStack.push(item);
            } else if(item.equals("*")) {
                operationStack.push(item);
            } else if(item.equals("/")) {
                operationStack.push(item);
            } else if(item.equals(")")) {
                String v1 = valueStack.pop();
                String v2 = valueStack.pop();
                String operation = operationStack.pop();
                valueStack.push(String.format("(%s%s%s)",v2,operation,v1));
            } else {
                valueStack.push(item);
            }
        }
        System.out.println(valueStack.peek());
    }
}
